<template>
  <div class="my-container">
    <!-- 已登录状态 if(user) -->
    <div v-if="user" class="header user-info">
      <!-- 蓝色区域.用户信息区 -->
      <div class="base-info">
        <div class="left">
          <van-image
            round
            fit="cover"
            class="avatar"
            src="https://img.yzcdn.cn/vant/cat.jpeg"
          >
            <!-- <template slot="error">
              <img src="https://img.yzcdn.cn/vant/cat.jpeg" alt="默认头像" />
            </template> -->
          </van-image>
          <span class="name">{{ usr.name }}</span>
        </div>
        <div class="right">
          <van-button size="mini" round @click="$router.push('/profile')">
            编辑资料
          </van-button>
        </div>
      </div>
      <!-- 蓝色区域.统计区 -->
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{ usr.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ usr.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ usr.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ usr.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <!-- 未登录状态 -->
    <div v-else class="header not-login">
      <!-- 点击 登录注册 按钮，通过 路由管理器 切换到 登录组件 -->
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/mobile.png" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>

    <!-- 宫格按钮 -->
    <van-grid column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>

    <!-- 横排按钮 -->
    <div class="grid-nav"></div>
    <van-cell title="消息通知" is-link url="" />
    <van-cell title="实名认证" is-link url="" />
    <van-cell title="系统设置" is-link url="" />
    <van-cell v-show="user" @click="logout" title="退出" is-link url="" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUsrInfo } from '@/api/user.js'

export default {
  name: 'my',
  props: {},
  data() {
    return {
      usr: {}
    }
  },
  async created() {
    // 发送 请求到服务器，获取 当前登录用户的完整信息
    const { data: res } = await getUsrInfo()
    this.usr = res.data
  },
  methods: {
    // 1.退出方法------------------------------
    logout() {
      this.$dialog
        .confirm({
          title: '系统提示',
          message: '您确认要退出了吗？呜呜呜~~'
        })
        .then(() => {
          // 删除 vuex 和 localstorage 中的登录信息
          this.$store.commit('setUserToken', null)
          // 提示退出消息
          this.$toast.success('退出成功')
        })
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  components: {}
}
</script>

<style lang="less" scoped>
.my-container {
  .header {
    height: 361px;
    background-image: url('~@/assets/banner.png');
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 120px;
          height: 120px;
          margin-right: 23px;
          border: 4px solid #fff;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
      }
      .count {
        font-size: 36px;
      }
      .text {
        font-size: 23px;
      }
    }
  }
  // 宫格导航
  .grid-nav {
    .grid-item {
      height: 141px;
      i.iconfont {
        font-size: 45px;
      }
      span.text {
        font-size: 28px;
      }
      .icon-shoucang {
        color: #eb5253;
      }
      .icon-lishi {
        color: #ff9d1d;
      }
    }
  }
}
</style>
